﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.InteropServices;
using System.Security.Principal;

/// <summary>
/// ShortMessge 的摘要说明
/// </summary>
public class ShortMessge
{
    EIE.EieConnection EieConn = new EIE.EieConnection();

    public ShortMessge()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 单发短信 返回值为0发送成功
    /// </summary>
    /// <param name="OperatorID">企业信息机卡号</param>
    /// <param name="OperatorPsd">密码</param>
    /// <param name="sGateway">信息机</param>
    /// <param name="sGatewayPsd">信息机密码</param>
    /// <param name="sIP">服务器IP</param>
    /// <param name="DBName">登录数据库帐户</param>
    /// <param name="DBPsd">登录数据库密码</param>
    /// <param name="sMobile">手机号</param>
    /// <param name="sContent">短信内容</param>
    /// <param name="sLogin">发短信人员帐号</param>
    /// <returns></returns>
    public int SendOneSM(string OperatorID , string OperatorPsd ,string sGateway,string sGatewayPsd,string sIP,string DBName,string DBPsd, string sMobile,string sSMContent,string sLogin)
    {
        int iRet = 1;
        try
        {
            int iDataConn = EieConn.EieInitialize(sIP, DBName, DBPsd);     //连接数据库
            if (iDataConn == 0)
            {
                int iSMConn = EieConn.EieLogin(OperatorID, OperatorPsd);  //企业信息机卡号,密码
                if (iSMConn == 0)
                {
                    byte iStateReport = byte.Parse("1");
                    string sSendTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                    string sSourAddr = sGateway;
                    string sDestAddr = sMobile;
                    string sServiceID = sGatewayPsd;
                    string sFeeType = "01";
                    string sFeeCode = "1";
                    string sContent = sSMContent;
                    string sCreatorID = sLogin; //  发送短信的用户帐号
                    byte iSMType = byte.Parse("0");
                    string sMessageID = "0";
                    byte iDestAddrType = byte.Parse("0");

                    EIE.SubmitSM SubmitSM = new EIE.SubmitSMClass();
                    SubmitSM.AtTime = sSendTime;
                    SubmitSM.SourceAddr = sSourAddr;
                    SubmitSM.DestAddr = sDestAddr;
                    SubmitSM.Content = sContent;
                    SubmitSM.StateReport = iStateReport;
                    SubmitSM.ServiceID = sServiceID;
                    SubmitSM.FeeType = sFeeType;
                    SubmitSM.FeeCode = sFeeCode;
                    SubmitSM.SMType = iSMType;
                    SubmitSM.MessageID = sMessageID;
                    SubmitSM.DestAddrType = iDestAddrType;

                    iRet = EieConn.SubmitSM(SubmitSM);   //发送短信
                }
            }
            EieConn.EieUninitialize();         //释放接口
            EieConn.EieLogout();               //注销
        }
        catch
        {
            EieConn.EieUninitialize();         //释放接口
            EieConn.EieLogout();               //注销
        }
        return iRet;   
    }
}
